<template>
    <div>
        <el-form ref="customer-form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="客户名称" prop="customerName">
            <el-input v-model="form.customerName" placeholder="请输入客户名称" />
          </el-form-item>
          <el-form-item label="付款方式" prop="paymentType">
            <el-select v-model="form.paymentType" placeholder="请选择付款方式" style="width:100%">
              <el-option
                  v-for="dict in paymentTypeOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                />
            </el-select>
          </el-form-item>
          <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm" size="mini">确 定</el-button>
          <el-button @click="cancel" size="mini">返 回</el-button>
          <el-button type="success" @click="addContract" size="mini" v-if="showContract">添加联系人</el-button>
        </div>
        <div style="height:5px;"></div>
        <el-table v-if="showContract" v-loading="loading" :data="contactList">
            <!-- <el-table-column type="selection" width="55" align="center" /> -->
            <el-table-column label="姓名" align="center" prop="contactName" />
            <el-table-column label="电话" align="center" prop="contactPhone" />
            <el-table-column label="地址" align="center" prop="contactAddress" />
            <el-table-column label="邮箱" align="center" prop="contactMail" />
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
                <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                >修改</el-button>
                <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                >删除</el-button>
            </template>
            </el-table-column>
        </el-table>

        <!-- 添加或修改客户联系人对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
            <el-form ref="contact-form" :model="contractForm" :rules="contractRules" label-width="80px">
                <el-form-item label="姓名" prop="contactName">
                <el-input v-model="contractForm.contactName" placeholder="请输入姓名" />
                </el-form-item>
                <el-form-item label="电话" prop="contactPhone">
                <el-input v-model="contractForm.contactPhone" placeholder="请输入电话" />
                </el-form-item>
                <el-form-item label="地址" prop="contactAddress">
                <el-input v-model="contractForm.contactAddress" placeholder="请输入地址" />
                </el-form-item>
                <el-form-item label="邮箱" prop="contactMail">
                <el-input v-model="contractForm.contactMail" placeholder="请输入邮箱" />
                </el-form-item>
                <el-form-item label="备注" prop="remark">
                <el-input v-model="contractForm.remark" placeholder="请输入备注" />
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="contractSubmitForm">确 定</el-button>
                <el-button @click="contractCancel">取 消</el-button>
            </div>
        </el-dialog>

    </div>
</template>
<script>
import { getCustomer, addCustomer, updateCustomer} from "@/api/business/customer"; 
import { listContact, getContact, delContact, addContact, updateContact, exportContact } from "@/api/business/contact";   
export default {
    name: 'CustomerEdit',
    props: {
        editConfig: { type: Object, default: () => ({}) }
    },
    data() {
        return {
            form: { },
            // 表单校验
            rules: {
                customerName: [
                    { required: true, message: "客户名称不能为空", trigger: "blur" }
                ],
            },
            id: '',
            contactList: [],
            // 付款方式数据字典
            paymentTypeOptions: [],

            // 显示联系人
            showContract:false,
            // // 选中数组
            // ids: [],
            // // 非单个禁用
            // single: true,
            // // 非多个禁用
            // multiple: true,
            // 遮罩层
            loading: true,
            // 客户联系人表格数据
            contactList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 999,
                customerId: null,
                contactName: null,
                contactPhone: null,
                contactAddress: null,
                contactMail: null,
            },
            // 联系人表单
            contractForm: { },
            // 联系人表单校验
            contractRules: {
                contactName: [
                    { required: true, message: "姓名不能为空", trigger: "blur" }
                ],
                contactPhone: [
                    { required: true, message: "电话不能为空", trigger: "blur" }
                ]
            },
        }
    },
    created() {
        this.getDicts("bus_payment_type").then(response => {
            this.paymentTypeOptions = response.data;
        });
    },  
    mounted(){
        if(this.editConfig.id != ''){
            this.id = this.editConfig.id
            getCustomer(this.id).then(response => {
                this.form = response.data;
            });
            this.queryParams.customerId = this.id
            this.showContract=true
            this.getContactList();
        }
    },
    methods: {
        // 取消按钮
        cancel() {
            this.$emit('onEditBack', false)
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["customer-form"].validate(valid => {
                if (valid) {
                    if (this.form.customerId != null) {
                        updateCustomer(this.form).then(response => {
                            this.msgSuccess("修改成功");
                        });
                    } else {
                        addCustomer(this.form).then(response => {
                            this.msgSuccess("新增成功");
                            console.log(response)
                            this.queryParams.customerId = response.data.customerId
                            this.id = response.data.customerId
                            this.showContract=true
                            this.getContactList();
                        });
                    }
                }
            });
        },

        /** 查询客户联系人列表 */
        getContactList() {
            this.loading = true;
            listContact(this.queryParams).then(response => {
                this.contactList = response.rows;
                this.loading = false;
            });
        },
        // 多选框选中数据
        // handleSelectionChange(selection) {
        //     this.ids = selection.map(item => item.contactId)
        //     this.single = selection.length!==1
        //     this.multiple = !selection.length
        // },
        // 表单重置
        resetContract() {
            this.contractForm = {
                contactId: null,
                customerId: null,
                contactName: null,
                contactPhone: null,
                contactAddress: null,
                contactMail: null,
                remark: null
            };
            this.resetForm("contact-form");
        },
        /** 新增按钮操作 */
        addContract(){
            this.resetContract();
            this.open = true;
            this.title = "添加客户联系人";
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.resetContract();
            const contactId = row.contactId || this.ids
            getContact(contactId).then(response => {
                this.contractForm = response.data;
                this.open = true;
                this.title = "修改客户联系人";
            });
        },
        // 取消按钮
        contractCancel() {
            this.open = false;
            this.resetContract();
        },
        /** 提交按钮 */
        contractSubmitForm() {
            this.$refs["contact-form"].validate(valid => {
                if (valid) {
                    if (this.contractForm.contactId != null) {
                        updateContact(this.contractForm).then(response => {
                        this.msgSuccess("修改成功");
                        this.open = false;
                        this.getContactList();
                        });
                    } else {
                        this.contractForm.customerId = this.id
                        addContact(this.contractForm).then(response => {
                        this.msgSuccess("新增成功");
                        this.open = false;
                        this.getContactList();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const contactIds = row.contactId || this.ids;
            this.$confirm('是否确认删除客户联系人的数据项?', "警告", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
                }).then(function() {
                return delContact(contactIds);
                }).then(() => {
                this.getContactList();
                this.msgSuccess("删除成功");
                }).catch(() => {});
        },
    }
}
</script>
